<?php

namespace App\Http\Controllers;

use App\WxUserInfo;
use Illuminate\Http\Request;

class SmallApp extends Controller
{
    /*
     * 登录
     * 请求方式 post
     * 请求参数 code nickname img
     */
    public function login(Request $request){
//        return 1;
        //实例化
        $model = new WxUserInfo();
        //调用方法 获取openid
        $json = $model->getOpenid($request['code']);
        $arr = json_decode($json,true);
//        return $arr['openid'];
        //防止重复登录
        $count=$model->uniqueAddData($arr['openid']);
        if ($count>0){
            //添加日志
//            $this->file_logs($arr['openid'],$request['nickname'],'登录');
            //添加积分
            $model->addScore($arr['openid']);
            return ['code'=>'0','msg'=>'登录成功','result'=>$arr['openid']];
        }
        $res=$model->insertData($arr['openid'],$request['nickname'],$request['img']);
        if ($res){
            //添加日志
//            $this->file_logs($arr['openid'],$request['nickname'],'注册');
            return ['code'=>'0','msg'=>'注册成功','result'=>$arr['openid']];
        }else{
            return ['code'=>'1','msg'=>'注册失败','result'=>null];
        }
    }
}
